package pacBioSimple;




import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PacBioMAFFTCall implements Callable<String> {

	int id=-1;
	String outputFile="";
	StringBuffer fastaformat = null;
	String name ="";
	
	public PacBioMAFFTCall(int id,String outputFile,StringBuffer fasta,String name) {
		this.id=id;
		this.outputFile = outputFile;
		this.fastaformat = fasta;
		this.name = name;
	}
	
	
	class StreamGobbler implements Callable<List<String>>
	{
		InputStream is;
		boolean type;
		public StreamGobbler(InputStream is) {
			
			this.is =is;
			
			
			// TODO Auto-generated constructor stub
		}
		@Override
		public List<String> call() throws Exception {
			// TODO Auto-generated method stub
			
			List<String> fastaSequence = new ArrayList<>();
			BufferedReader brf = new BufferedReader(new InputStreamReader(is));
			String line ="";
			StringBuffer sb = new StringBuffer();
			boolean fasta = false;
			while((line=brf.readLine())!=null)
			{
				//System.out.println(line);
	
				if(line.length()>0)
				{
					if(line.charAt(0)=='>')
					{
						fasta=true;
						if(sb.length()>0)
						{
							//maskClipping(sb);
						//	System.out.println(sb.toString());
							fastaSequence.add(sb.toString());
							sb = new StringBuffer();
						}
					}
					else if(line.length()>0 && fasta)
					{
						sb.append(line);
					}
				}
			}
			if(sb.length()>0)
			{
				//maskClipping(sb);
				fastaSequence.add(sb.toString());
			}
					//System.out.println("done no error");
			brf.close();
			return fastaSequence;				
		}
		
	}
	
	
	@Override
	public String call() throws Exception {
		
		String filendir = this.outputFile+"_"+id;
		
	//first write the fasta file!
		
		PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(filendir))));
		pw.print(this.fastaformat);
		pw.flush();
		pw.close();
		
		//now we have to call MAFFT!
		StringBuffer command =new StringBuffer("perl /home/patel/git/filter/pacBio/sourc_code/linker/callMafft.pl -i "+filendir+" -o "+filendir+"_mafft_result");
		
		
		ExecutorService executor = Executors.newCachedThreadPool();
		
		Process p = MyUtil.callProcessAndGetProcess(command.toString());
				
//		List<Callable<List<String>>> inputStreams = new ArrayList<>();
//		inputStreams.add(new StreamGobbler(p.getErrorStream(), false));
//		inputStreams.add(new StreamGobbler(p.getInputStream(), true));
//			
//		List<Future<List<String>>> streamResults = executor.invokeAll(inputStreams);
		
		Future<List<String> > error = executor.submit(new StreamGobbler(p.getInputStream()));
		
		boolean finished=false;
		int exit_status=-1;
		while(!finished)
		{
			try
			{
				exit_status = p.exitValue();
				finished=true;				
			}
			catch(Exception e)
			{
				Thread.sleep(10);
			}
			
		}
		//System.out.print(exit_status);
		if(exit_status>0)
		{
			System.err.println("ERROR mafft CALL");
		}
	
		//now we have to read back the fasta file
		PacBioLinkerObject pblMafft = new PacBioLinkerObject(name);
		//pblMafft.readMULTIFasta(new BufferedReader(new InputStreamReader(p.getInputStream())));
		List<String > steamResults = error.get();
		executor.shutdown();
		pblMafft.readMultiFasta(steamResults);
		StringBuffer resultingSequence =pblMafft.compressToOneSeq();
		//executor.shutdown();
		return resultingSequence.toString();			
	}

}
